#include "main.h"
#include "time_display.h"
#include "debug_dialog.h"
#include <QNetworkInterface>
#include <QNetworkConfigurationManager>
#include "machine_failure_process.h"


machine_manage::machine_manage(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::machine_manage)
{
  ui->setupUi(this);
  ui->label_factory_time->setText(whole_machine_parameter.str_factory_time);
  ui->label_used_times->setText(QString::number(whole_machine_parameter.used_times_counter));
  ui->serial_number_label->setText(whole_machine_parameter.serial_number);
  test=NULL;
  sps=NULL;
  displayIP();

  ui->version->setText(__DATE__);
}

machine_manage::~machine_manage()
{
    delete ui;
    delete test;
}

void machine_manage::on_sterilization_setting_btn_clicked()
{      BUTTON_SOUND;

      stype->manage=1;
      stype->enter();
      //this->close();

}

void machine_manage::on_back_btn_clicked()
{
     BUTTON_SOUND;
     stype->manage=0;
     save_config(DATA_FILE);
     this->close();
     //w->showFullScreen();

}
void machine_manage::on_datatime_save_btn_clicked()
{
    if(ui->datatime_save_btn->isChecked())
    {
     ui->datatime_save_btn->setText(trUtf8("保存"));
     ui->datatime_save_btn->SET_COLOR_RED
     ui->datetimeedit->write_protect(false);
    }
    else
    {
    ui->datatime_save_btn->SET_COLOR_GREEN
    ui->datatime_save_btn->setText(trUtf8("修改"));
    ui->datetimeedit->write_protect(true);
    datatime= ui->datetimeedit->ui->dateTimeEdit->dateTime();
    QString time= datatime.toString(QString("MMddhhmmyyyy.ss"));
    system("date -s "+time.toAscii());
    system("hwclock -w");
    }
}

void machine_manage::on_sensor_btn_clicked()
{
    BUTTON_SOUND;
    MCD_Interface->set_datamap(&sensor_adjust_datamap,3);
    MCD_Interface->showFullScreen();
}

 void machine_manage::on_leak_test_btn_clicked()
 {
     if(!whole_machine_parameter.bottom_exhaust_machine)
   {
    BUTTON_SOUND;
    if(test==NULL) test=new leak_test;
    test->showFullScreen();
    }
 }


 void machine_manage::on_special_setting_btn_clicked()
  {
     BUTTON_SOUND;
//     connect(sps,SIGNAL(value_changed()),SLOT(back_clicked()));
     if(input->password_check(supper_password,false))
     {     if(sps==NULL) sps=new special_setting;
            sps->showFullScreen();
     }
  }

 void  machine_manage::on_read_history_record_btn_clicked()
 {
   docreader reader;
   reader.exec();

 }

 void machine_manage::on_repair_mode_btn_clicked()
 {
     BUTTON_SOUND;
     machine_failure_process failure_process;
     failure_process.exec();
 }

 void  machine_manage::on_debug_btn_clicked()
 {

   if(input->password_check(supper_password,false))  debug.entrance();

 }

 void machine_manage::on_networking_btn_clicked()
 {
    BUTTON_SOUND
     if(cmd.isOpen())   return ;
     QString ip;
     cmd.start("/sbin/udhcpc &");
     QTimer::singleShot(10000, this, SLOT(displayIP()));

 }

 void machine_manage::displayIP()
 {
     QString ip;
     cmd.start("ifconfig eth0");
     cmd.waitForFinished(100);

     if( cmd.readAll().contains( "192.168") )
     {
       ip=QNetworkInterface::allAddresses().at(1).toString();
     }
     else ip=trUtf8("网络未连接！");
     cmd.close();
     ui->IpAddress_label->setText(ip);


 }
